from vnpy_ctastrategy.template import(
    CtaTemplate
)
from vnpy.trader.object import(
    BarData,
    TickData,
    TradeData,
    OrderData
)

from vnpy.trader.utility import(
    BarData,
    
)

# from vnpy.trader.utility import(
#     BarGenerator,
#     ArrayManager
# )
from typing import Any

class LiangCha(CtaTemplate):
    """"""

    author="LiangCha"

    isBusy=False
    status=1
    lastPrice=0

    myprice=0
    myPos=0
    stopSunPrice=0
    stopYingPrice=0
    priceNow=0
    benifit=0

    volCha=0
    volChaNow=0

    parameters=[  
        "volCha"
    ]

    variables=[
        "myPos",
        "volChaNow",
        "myprice",
        "priceNow",
        "benifit",
        "stopSunPrice",
        "stopYingPrice"
        
    ]

    def __init__(self, cta_engine: Any, strategy_name: str, vt_symbol: str, setting: dict) -> None:
        super().__init__(cta_engine, strategy_name, vt_symbol, setting)
        # self.bg = BarGenerator(self.on_bar)
        # self.am = ArrayManager()

    def on_init(self) -> None:
        self.write_log("LiangCha 策略初始化")
        # self.load_bar(10)

    def on_start(self) -> None:
        self.myPos=0
        # self.startTrade=1
        self.isBusy=False
        
        self.write_log("LiangCha 策略启动")
    
    def on_stop(self) -> None:
        # self.startTrade=0
        self.write_log("LiangCha 策略停止")

    def on_trade(self, trade: TradeData) -> None:
        print(trade)
        return super().on_trade(trade)

    def on_order(self, order: OrderData) -> None:
        print(order)
        return super().on_order(order)

    def on_tick(self, tick: TickData) -> None:
        # stopYingPrice=self.stopYingPrice
        # self.write_log("pos=%s,volCha=%s" % (pos,tick.ask_volume_1-tick.bid_volume_1))

        # 1 变量赋值
        self.volChaNow=tick.bid_volume_1-tick.ask_volume_1


        if self.trading:
            if not self.isBusy:
                self.isBusy=True
                if self.pos==0:
                    self.priceNow=tick.bid_price_1
                    if self.volChaNow<=self.volCha:
                        self.myPos=-1
                        rs=self.short(tick.bid_price_1, 1)
                        self.write_log(rs)
                        self.myprice=tick.bid_price_1
                        self.stopSunPrice=self.myprice+2
                        self.stopYingPrice=self.myprice-1
                        self.write_log("开空,pos=%s,volCha=%s,myprice=%s，stopSunPrice=%s,stopYingPrice=%s" % (self.myPos,tick.ask_volume_1-tick.bid_volume_1,self.myprice,self.stopSunPrice,self.stopYingPrice))
                    # else:
                    #     self.write_log("空等,pos=%s,volCha=%s" % (self.myPos,tick.ask_volume_1-tick.bid_volume_1))
                    # elif tick.bid_volume_1-tick.ask_volume_1>=500:
                    #     pos=-1
                    #     self.short(tick.ask_price_1, 1 , stop=True)
                    #     myprice=tick.ask_price_1
                    #     stopSunPrice=myprice+2
                    #     self.write_log("开空 price="+myprice)
                elif self.myPos<0:
                    self.benifit=self.myprice-tick.ask_price_1
                    self.priceNow=tick.ask_price_1
                    if tick.ask_price_1>=self.stopSunPrice:
                        self.cover(tick.ask_price_1, 1)
                        self.myPos=0
                        self.write_log("止损,pos=%s,priceNow=%s,stopSunPrice=%s,盈利=%s" % (self.myPos,tick.ask_price_1,self.stopSunPrice,self.benifit))
                        self.myprice=0
                    elif tick.ask_price_1<=self.stopYingPrice:
                        self.cover(tick.ask_price_1, 1)
                        self.myPos=0
                        self.write_log("止盈,pos=%s,myprice=%s,priceNow=%s,stopYingPrice=%s,盈利=%s" % (self.myPos,self.myprice,tick.ask_price_1,self.stopYingPrice,self.benifit))
                        self.myprice=0
                    # elif tick.bid_price_1-2>self.stopSunPrice:
                    #     self.stopSunPrice=tick.bid_price_1-2
                    #     self.write_log("止涨,pos=%s,myprice=%s,price=%s,stopSunPrice=%s,盈利=%s" % (self.myPos,self.myprice,tick.bid_price_1,self.stopSunPrice,self.myprice-tick.bid_price_1))
                    else:
                        if self.lastPrice!=tick.ask_price_1:
                            self.write_log("保持,pos=%s,myprice=%s,priceNow=%s,stopSunPrice=%s,盈利=%s" % (self.myPos,self.myprice,tick.ask_price_1,self.stopSunPrice,self.benifit))
                            self.lastPrice=tick.ask_price_1
                
                self.isBusy=False
            else:
                self.write_log("isBusy=%s" % (tick.datetime))
        self.put_event()
            

    # def myLog(self,action,priceIn,priceOut,tick) ->None:
    #     self.write_log("仓位="+str(self.myPos)+",动作="+action+",入手价="+str(self.myprice)+",止损价="+str(self.stopSunPrice)+",现价="+str(tick.ask_price_1)+",出手价="+str(priceOut)+",盈利="+str(priceOut-self.myprice))
